﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class Options : ChildWindow
    {
        public int menuItemID, max_precedence=0, currentSelectedIndex=-1;

        public Options()
        {
            InitializeComponent();
        }

        public void initOptionGroupItems()
        {
            lbOptionGroups.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.MenuItemOptionGroup miog in Globals.db.MenuItemOptionGroupRecords)
            {
                try
                {
                    var optionGroup = Globals.db.OptionGroupRecords.Single(i => i.col_ID == miog.col_OptionGroupID);
                    lbOptionGroups.Items.Add(optionGroup.col_Name);
                    max_precedence=miog.col_Precedence;
                }
                catch (Exception) { }
            }
            if (currentSelectedIndex < lbOptionGroups.Items.Count)
                lbOptionGroups.SelectedIndex = currentSelectedIndex;
        }

        public async void init()
        {
            // Load option groups for the selected menu item
            await Globals.db.GetMenuItemOptionGroupObjects("MenuItemID=" + menuItemID + " order by Precedence");
            initOptionGroupItems();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private async void btnRemoveOptionGroup_Click(object sender, RoutedEventArgs e)
        {
            if (lbOptionGroups.SelectedIndex >= 0)
            {
                // Remove selected option group
                await Globals.db.DeleteMenuItemOptionGroupObject(Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex]);
                init();
            }
        }

        private void btnAddOptionGroup_Click(object sender, RoutedEventArgs e)
        {
            OptionGroupSelect ogs = new OptionGroupSelect();
            ogs.options = this;
            ogs.init();
            ogs.Show();
        }

        private async void btnUpOptionGroup_Click(object sender, RoutedEventArgs e)
        {
            if (lbOptionGroups.SelectedIndex > 0)
            {
                currentSelectedIndex = lbOptionGroups.SelectedIndex - 1;
                short precedence = Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex - 1].col_Precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex - 1].col_Precedence =
                    Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].col_Precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].col_Precedence = precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex-1].bFilterPrecedence = true;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].bFilterPrecedence = true;
                await Globals.db.UpdateMenuItemOptionGroupObject(Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex - 1]);
                await Globals.db.UpdateMenuItemOptionGroupObject(Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex]);
                await Globals.db.GetMenuItemOptionGroupObjects("MenuItemID=" + menuItemID + " order by Precedence");
                initOptionGroupItems();
            }
        }

        private async void btnDownOptionGroup_Click(object sender, RoutedEventArgs e)
        {
            if (lbOptionGroups.SelectedIndex >= 0 && lbOptionGroups.SelectedIndex < lbOptionGroups.Items.Count()-1)
            {
                currentSelectedIndex = lbOptionGroups.SelectedIndex + 1;
                short precedence = Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex + 1].col_Precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex + 1].col_Precedence =
                    Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].col_Precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].col_Precedence = precedence;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex + 1].bFilterPrecedence = true;
                Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex].bFilterPrecedence = true;
                await Globals.db.UpdateMenuItemOptionGroupObject(Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex + 1]);
                await Globals.db.UpdateMenuItemOptionGroupObject(Globals.db.MenuItemOptionGroupRecords[lbOptionGroups.SelectedIndex]);
                await Globals.db.GetMenuItemOptionGroupObjects("MenuItemID=" + menuItemID + " order by Precedence");
                initOptionGroupItems();
            }
        }
    }
}

